﻿
/***************************************************************************

Copyright (c) BizRulX. All rights reserved.

***************************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Reflection;

namespace RuleXSoft.BizRuleCreator
{
    public class ValidationAttributes<T>
    {
        private T _sourceObj;
        
        

        public ValidationAttributes(T itemObj)
        {
            if (itemObj!=null)
            {
                _sourceObj = itemObj; 
            }
        }

        public ValidationAttributes()
        {
            
        }

        public override bool Equals(object obj)
        {
            bool _returnFlag = false;

            if ((obj == null) || (GetType() != obj.GetType()))
                return false;
            T _issueParamObj = (T)obj;
            
            Type _oType = typeof(T);
            PropertyInfo[] propInfos = _oType.GetProperties();

            PropertyInfo[] properties = obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);

            foreach (PropertyInfo property in properties)
            {
                object[] customAtt = property.GetCustomAttributes(typeof(ValidationAttribute), true);
                foreach (System.Attribute attr in customAtt)
                {
                    //if (attr is DirectionAttribute)
                    //{
                    DirectionAttribute dirAtt = attr as DirectionAttribute;
                    if (dirAtt == null) continue;

                    //if (!dirAtt.Output)
                    //{
                    //    _returnFlag = true;
                    //}

                    if (!dirAtt.Output)
                    {
                        foreach (PropertyInfo propInfo in propInfos)
                        {
                            if (Convert.ToString(property.Name).Equals(Convert.ToString(propInfo.Name)))
                            {
                                if (property.GetValue(obj, null).Equals(propInfo.GetValue(this, null)))
                                {
                                    _returnFlag = true;
                                }
                                else
                                {
                                    _returnFlag = false;
                                    return false;
                                    //break;
                                } 
                            }
                        }
                    }
                }

                

            }

            return _returnFlag;
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }
    }
}
